<?php
use app\common\service\JwtService;
use app\common\RootException;
use app\backend\model\AdminModel;

/**
 * 获取登录账户的信息
 * @param string|null $field 需要获取字段的值
 * @return mixed|object|\think\App
 * @throws RootException
 */
if (!function_exists('adminInfo')) {
    function adminInfo(string $field = null) {
        // 提取cookie里面的token
        $accessToken = cookie('accessToken');
        // 提取JWT载荷中的用户ID
        $claimData = (new JwtService())->parseToken(cache($accessToken))->get('data');
        // 从缓存取出账户信息
        $adminInfo = cache(md5($accessToken . $claimData['userId']));
        if (empty($adminInfo)) {
            return $adminInfo;
        }
        // 字段筛选
        if (!empty($field) && !array_key_exists($field, $adminInfo)) {
            cookie('accessToken', null);
            return header("Location: " . url('Login/index'), true, 302);
            // throw new RootException("用户信息中不存在【{$field}】字段");
        }
        // 根据传入字段返回对应值
        return empty($field) ? $adminInfo : $adminInfo[$field];
    }
}

/**
 * 获取所有子级管理员信息
 * @param string|null $field 需要筛选的字段
 * @param false $hasSelf 是否包含自己
 * @return array
 * @throws RootException
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\DbException
 * @throws \think\db\exception\ModelNotFoundException
 */
if (!function_exists('selfSubAdminList')) {
    function selfSubAdminList(string $field = null, bool $hasSelf = false) {
        // 数据源
        $adminList = infiniteRecursionAdmin(AdminModel::select()->toArray(), adminInfo('id'));
        // 是否包含自己
        $hasSelf && array_unshift($adminList, adminInfo());
        // 根据传参返回对应的列表
        return empty($field) ? $adminList : array_unique(array_column($adminList, $field));
    }
}

/**
 * 无限递归筛选子用户
 * @param array $allAdmins 所有管理员表数据
 * @param int $selfAdminId 需要递归查询的管理员ID
 * @return array
 */
if (!function_exists('infiniteRecursionAdmin')) {
    function infiniteRecursionAdmin(array $allAdmins, int $selfAdminId, int $level = 0) {
        $returnData = [];
        foreach ($allAdmins as $key => $adminInfo) {
            if ($adminInfo['admin_id'] == $selfAdminId) {
                $adminInfo['level'] = $level;
                $returnData[] = $adminInfo;
                unset($allAdmins[$key]);
                $returnData = array_merge($returnData, infiniteRecursionAdmin($allAdmins, $adminInfo['id'], $level + 1));
            }
        }
        return $returnData;
    }
}

/**
 * 无限递归筛选子菜单
 * @param array $allAdmins 所有管理员表数据
 * @param int $selfAdminId 需要递归查询的管理员ID
 * @return array
 */
if (!function_exists('infiniteRecursionMenus')) {
    function infiniteRecursionMenus(array $menuData, int $parentId = 0, int $level = 0) {
        $returnData = [];
        foreach ($menuData as $key => $menu) {
            if ($menu['parent_id'] == $parentId) {
                $menu['level'] = $level;
                $menu['list'] = infiniteRecursionMenus($menuData, $menu['id'], $level + 1);
                $returnData[] = $menu;
            }
        }
        return $returnData;
    }
}
